package com.paperless.model;

import com.blankj.utilcode.util.LogUtils;
import com.paperless.sdk.BuildConfig;

import java.nio.ByteBuffer;

/**
 * @author : Administrator
 * date : 2022/9/21 18:51
 * description :
 */
public class FrameData {
    int isKeyFrame;
    int res;
    int codecid;
    int w;
    int h;
    long pts;
    byte[] bytes;
    int packetSize;
    byte[] codecdata;
    int codecdataSize;

    public FrameData() {
        if (BuildConfig.isUseNewWayPlay) {
            bytes = new byte[1024 * 500];
            codecdata = new byte[600];
        }
    }

    public FrameData(int isKeyFrame, int res, int codecid, int w, int h, long pts, ByteBuffer packet, ByteBuffer codecbf) {
        this.isKeyFrame = isKeyFrame;
        this.res = res;
        this.codecid = codecid;
        this.w = w;
        this.h = h;
        this.pts = pts;
        this.packetSize = packet.remaining();
        this.codecdataSize = codecbf.remaining();
        packet.get(bytes, 0, this.packetSize);
        codecbf.get(codecdata, 0, this.codecdataSize);
    }

    public boolean isKeyFrame() {
        return (isKeyFrame & 4) == 4;
    }

    public int getIsKeyFrame() {
        return isKeyFrame;
    }

    public void setIsKeyFrame(int isKeyFrame) {
        this.isKeyFrame = isKeyFrame;
    }

    public int getRes() {
        return res;
    }

    public void setRes(int res) {
        this.res = res;
    }

    public int getCodecid() {
        return codecid;
    }

    public void setCodecid(int codecid) {
        this.codecid = codecid;
    }

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    public long getPts() {
        return pts;
    }

    public void setPts(long pts) {
        this.pts = pts;
    }

    public byte[] getBytes() {
        return bytes;
    }

    public void setBytes(byte[] bytes) {
        try {
            if (!BuildConfig.isUseNewWayPlay) {
                this.bytes = bytes;
            } else {
                System.arraycopy(bytes, 0, this.bytes, 0, bytes.length);
            }
            this.packetSize = bytes.length;
        } catch (Exception e) {
            LogUtils.e("出现异常：" + e);
        }
    }

    public int getPacketSize() {
        return packetSize;
    }

    public void setPacketSize(int packetSize) {
        this.packetSize = packetSize;
    }

    public byte[] getCodecdata() {
        return codecdata;
    }

    public void setCodecdata(byte[] codecdata) {
        if (!BuildConfig.isUseNewWayPlay) {
            this.codecdata = codecdata;
        } else {
            System.arraycopy(codecdata, 0, this.codecdata, 0, codecdata.length);
        }
        this.codecdataSize = codecdata.length;
    }

    public int getCodecdataSize() {
        return codecdataSize;
    }

    public void setCodecdataSize(int codecdataSize) {
        this.codecdataSize = codecdataSize;
    }

    public void setPacketBuffer(ByteBuffer packetBuffer) {
        this.packetSize = packetBuffer.remaining();
        packetBuffer.get(bytes, 0, this.packetSize);
    }

    public void setCodecbf(ByteBuffer codecbf) {
        this.codecdataSize = codecbf.remaining();
        codecbf.get(codecdata, 0, this.codecdataSize);
    }

    @Override
    public String toString() {
        return "FrameData{" +
                "isKeyFrame=" + isKeyFrame +
                ", res=" + res +
                ", codecid=" + codecid +
                ", w=" + w +
                ", h=" + h +
                ", pts=" + pts +
                ", packetSize=" + packetSize +
                ", codecdataSize=" + codecdataSize +
                '}';
    }
}
